import { AbsFormUtil } from '@abstract/form';
import { Component, OnInit } from '@angular/core';
import { FormBuilder, Validators } from '@angular/forms';
import { UserService } from '@service/user.service';
import { deepClone } from '@utils/lang';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzModalRef } from 'ng-zorro-antd/modal';

@Component({
	selector: 'app-user-index-batch',
	templateUrl: './user-index-batch.component.html',
	styleUrls: ['./user-index-batch.component.less']
})
export class UserIndexBatchComponent extends AbsFormUtil implements OnInit {

	warehouseNodes = [];
	userList = [];
	toolTypeList = [];

	constructor(
		private fb: FormBuilder,
		private nzModalRef: NzModalRef,
		private nzMessageService: NzMessageService,
		private userService: UserService,
	) {
		super();
	}

	ngOnInit() {
		this.buildForm();
		this.getUserList();
	}


	destroyModal() {
		this.nzModalRef.destroy();
	}

	private async getUserList() {
		const result = await this.userService.getUserList({page: 0, size: 1000});
		this.userList = result.content;
	}

	private changeWarehouse(toolTypeOpt: any[]) {
		toolTypeOpt.forEach(it => {
			it.isLeaf = !it.hasChildrenFlag;
			it.value = it.id;
			it.title = it.name;
			it.key = it.id;
			if (it?.children?.length) {
				this.changeWarehouse(it.children);
			}
		})
	}
	onSubmit() {
		this.markAsDirty();
		if (this.validateForm.invalid) {
			return;
		}
		const resFormVal = deepClone(this.validateForm.value);
		return { ...resFormVal };
	}
	buildForm() {
		this.validateForm = this.fb.group({
			userIdList: [[], [Validators.required]],
			warehouseIdList: [[]],
			typeIdList: [[]]
		});
	}


}
